#include<bits/stdc++.h>
using namespace std;
const int N=1e7+7,M=998244353;
using ll=long long;
using ul=unsigned long long;
using vt=vector<int>;
int T,n,m,a[N],b[N],A,B,C;
void del(int &x,int y){if((x-=y)<0)x+=M;}
int qp(ll a,int x=M-2){
    int res=1;for(;x;x>>=1,a=a*a%M)
        if(x&1)res=a*res%M;return res;
}
struct dat{
    int x,y;
    bool operator<(const dat &z)const{
        return 1ll*x*z.y<1ll*y*z.x;
    }
    dat operator-(const dat &z)const{
        return{x-z.x,y-z.y};
    }
}ans,q[N];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    freopen("guess.in","r",stdin);
    freopen("guess.out","w",stdout);
    int i,j,k,l,r,x,y,z;
    for(cin>>T;T--;){
        cin>>n>>k>>A>>B>>C>>m;
        for(x=1;x<=m;++x)a[x]=0;
        for(x=1;x<=n;++x)
            ++a[k=((1ll*k*A+B)%m*k+C)%m+1];
        for(i=1,k=m,m=0;i<=k;++i)
            if(a[i])a[++m]=a[i]>1?3:2;
        for(i=1;i<=m;++i)a[i]+=a[i-1];
        ans={1,1};
        q[l=r=1]={0,0};
        for(x=1;x<=m;++x){
            dat at={x,a[x]-2};
            while(l<r&&(at-q[l+1])<(at-q[l]))++l;
            ans=min(ans,at-q[l]);
            at={x,a[x]};
            while(l<r&&(q[r]-q[r-1])<(at-q[r]))--r;
            q[++r]=at;
        }
        k=1ll*ans.x*qp(ans.y)%M;
        printf("%.5lf %d\n",(double)ans.x/ans.y,k);
    }
    return 0;
}